/**
 * create by young (young2f@outlook.com) on 2020/9/12
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Calendar, Form, Table} from 'antd';
import styles from '../index.less';
import DirTree from '../../components/DirTree';
import FormBase from '../../components/Base/FormBase';
import notify from "../../components/NotificationExtra";
import moment from "moment";
import ViewTitle from "../../components/ViewTitle";
import {ModalView} from "antd-extra";
import ModalCreateOrEdit from "./modal/ModalCreateOrEdit";
import Button from "antd/es/button";

const _ = require('lodash');

const table = 'DicAccount';

@Form.create()
@connect(({
            tenant,
            global,
            dynamicLabel,
            dic
          }) => ({...tenant, ...global, ...dic, ...dynamicLabel}))
export default class Bid extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.dispatch && this.props.dispatch({
      type: 'global/getModel',
      payload: {table, tenantCode: this.getTenantCode()},
    });
    this.onLoadData().then(data => {
      this.setState(data);
    });
  }

  async onLoadData() {

    const res = await this.props.dispatch({
      type: 'dic/findMany',
      payload: {table},
    });

    const data = res && res.result ? res.result : [];
    return {data};
  };

  onDelete = record => {
    const {code} = record;
    this.props.dispatch({
      type: 'dic/deleteOne',
      payload: {table, code},
    }).then(res => {
      this.requestReturn(res);
      this.componentDidMount();
    });
  };
  onCreateOrEdit = record => {
    const title = record ? '编辑记录' : "创建记录";
    const type = record ? "dic/updateOne" : "dic/create";
    ModalView.open(ModalCreateOrEdit,
      {
        title,
        width: '1080px',
      }, {
        record, table,
        callback: (payload) => {
          payload.table = table;
          payload.code = record ? record.code : undefined;
          this.props.dispatch({
            type,
            payload,
          }).then(res => {
            this.requestReturn(res);
            this.componentDidMount();
          });
        }
      });
  };

  render() {
    const {_notify} = this.props;
    if (_notify) {
      notify(_notify);
    }
    const {data} = this.state;
    const model = this.props[`${table}Model`];

    return (
      <Form className={styles.content}>
        <ViewTitle title={'客户账号列表'}
                   actions={<Button type="primary"
                                    onClick={() => this.onCreateOrEdit()}
                   >添加</Button>}
        ></ViewTitle>
        <Table
          className={styles.antTable}
          bordered={true}
          columns={this.getDicTableCols(model, this.onCreateOrEdit, this.onDelete)}
          dataSource={data}
          scroll={{x: true}}
          pagination={true}
        ></Table>
      </Form>
    );
  }
}
